package be.kuleuven.mume.rest;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import be.kuleuven.mume.JSONHelper;
import be.kuleuven.mume.beans.ProfBean;
import be.kuleuven.mume.beans.ProfBean.ProfBeanFactory;

@Path("/prof")
public class Prof {
	
	private static final Logger log = Logger.getLogger(Prof.class.getName());
	@Path("/")
	@GET
	@Produces("application/json")
	public String doGet() throws JSONException{
		
		try{
			JSONArray array = new JSONArray();
			List<ProfBean> proffen = ProfBeanFactory.getProffen();
			
			for (ProfBean profBean : proffen) {
				array.put((new JSONObject(profBean)));
			}
			
			JSONObject result = new JSONObject();
			result.put("result", array);
			
			return result.toString();
		} catch(JSONException e) {
			log.log(Level.SEVERE, "JSONException", e);
		}
		return JSONHelper.ERROR;
	}
	
	@Path("/add")
	@POST
	@Produces("text/plain")
	public String doPost(@FormParam(value = "name") String name, @FormParam(value = "mail")String mail, @FormParam(value="twitterName")String twitter){
		ProfBean prof = new ProfBean();
		prof.setName(name);
		prof.setMail(mail);
		prof.setTwitterName(twitter);
		prof.store();
		return "Succes";
	}
}
